package zuoye;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TicksRunnable implements Runnable{
    int ticks=100;

    @Override
    public void run() {
        while (true){
            synchronized (TicksRunnable.class){
                if (ticks>0){
                    System.out.println(Thread.currentThread().getName()+"-->"+ticks--);
                }
                if (ticks<=0)break;
            }
            if (ticks>0){
                System.out.println(Thread.currentThread().getName()+"-->"+ticks--);
            }
            if (ticks<=0)break;
        }
    }
}
class TestRunnable{
    public static void main(String[] args) {
        TicksRunnable target=new TicksRunnable();
      /*  new Thread(target).start();
        new Thread(target).start();
        new Thread(target).start();
        new Thread(target).start();
        new Thread(target).start();*/
        /*ExecutorService pool =*/ Executors.newFixedThreadPool(3);
        //ExecutorService是用来辅助创建线程池的工具类
        //newFixedThreadPool()这个方法可以创建指定数目线程的线程池对象
        for (int i = 0; i < 3; i++) {
            Executors.newFixedThreadPool(3).execute(target);
            //写法不一样，结果不同
        }
    }

}
